﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using MdxLib.Model;
using System.IO;

namespace UnityEngine.Graphics.Shader
{
    public class StandardShader : Shader
    {
        private Effect _Effect;

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_Effect != null)
                    _Effect.Dispose();
            }

            _Effect = null;

            base.Dispose(disposing);
        }

        public StandardShader(GraphicsDevice device)
            : base(device)
        {
        }

        public override void ResetDevice(GraphicsDevice device)
        {
            if (device == null)
                return;

            this.GraphicsDevice = device;
        }

        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager Content)
        {
            this._Effect = Content.Load<Effect>("Shader\\StandardBonesEffect");
        }

        public override void Render()
        {
            EffectManager.Instance.GraphicsDevice.Clear(Color.Transparent);

            SceneManager.Instance.Render3D();
            SceneManager.Instance.Render2D();

            SceneManager.Instance.ClearRenderQueue();
        }

        public override EMaterialLayerFilterMode FliterMode
        {
            set
            {
                Effect = this._Effect;
                this.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
                this.GraphicsDevice.BlendState = BlendState.Opaque;
                this.GraphicsDevice.RasterizerState = RasterizerState.CullNone;

                if (this.IsCreateShadow == true)
                {
                    this._Effect.CurrentTechnique = this._Effect.Techniques["CreateShadowMap"];
                }
                else
                {
                    switch (value)
                    {
                        case EMaterialLayerFilterMode.None:
                            this._Effect.CurrentTechnique = this._Effect.Techniques["NoneTechnique"];
                            break;
                        case EMaterialLayerFilterMode.Transparent:
                            this._Effect.CurrentTechnique = this._Effect.Techniques["TranslucentTechnique"];
                            break;
                        case EMaterialLayerFilterMode.Blend:
                            this._Effect.CurrentTechnique = this._Effect.Techniques["TransparentTechnique"];
                            break;
                        case EMaterialLayerFilterMode.Additive:
                            this._Effect.CurrentTechnique = this._Effect.Techniques["AdditiveTechnique"];
                            break;
                        case EMaterialLayerFilterMode.AdditiveAlpha:
                            this._Effect.CurrentTechnique = this._Effect.Techniques["AdditiveAlphaTechnique"];
                            break;
                        default:
                            this._Effect.CurrentTechnique = this._Effect.Techniques["AdditiveAlphaTechnique"];
                            break;
                        //throw new Exception("Error in setting blend mode.");
                    }
                }
            }
        }

        public override float Fog
        {
            set
            {
                this._Effect.Parameters["Fog"].SetValue(value);
            }
        }

        public override Matrix[] Bones
        {
            set
            {
                this._Effect.Parameters["Bones"].SetValue((Matrix[])value);
            }
        }

        public override bool UseBones
        {
            set
            {
                this._Effect.Parameters["UseBones"].SetValue(value);
            }
        }

        public override Matrix EyeViewProjection
        {
            set
            {
                this._Effect.Parameters["EyeViewProj"].SetValue(value);
            }
        }

        public override Matrix World
        {
            set
            {
                this._Effect.Parameters["World"].SetValue(value);
            }
        }

        public override Matrix View
        {
            set
            {
                this._Effect.Parameters["View"].SetValue(value);
            }
        }

        public override Matrix Projection
        {
            set
            {
                this._Effect.Parameters["Projection"].SetValue(value);
            }
        }

        public override Texture2D ShadowMap
        {
            set
            {
                this._Effect.Parameters["ShadowMap"].SetValue(value);
            }
        }

        public override Texture2D Texture
        {
            set
            {
                this._Effect.Parameters["Texture"].SetValue(value);
            }
        }

        public override float Alpha
        {
            set
            {
                this._Effect.Parameters["Alpha"].SetValue(value);
            }
        }

        public override bool TextureEnabled
        {
            set
            {
                this._Effect.Parameters["TextureEnabled"].SetValue(value);
            }
        }
    }
}
